<template>
  <div class="app-container app-content">
    <el-row>
      <el-col :span="24">
        <el-row class="line-bottom" v-show="showSearch">
          <el-col class="form_col">
            <el-form :model="queryParams" ref="queryForm" :size="size" v-show="showSearch" :label-width="labelWidth">
              <div id="searchFilter" :gutter="10" style="display: flex; flex-wrap: wrap">
                <!-- <el-form-item label="租户" prop="tenantId">
                  <el-select
                    v-model="queryParams.tenantId"
                    placeholder="请选择租户"
                    clearable
                  >
                    <el-option
                      v-for="dict in dict.type.belongDepartment"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    />
                  </el-select>
                </el-form-item> -->
                <el-form-item label="最小用量" prop="minDosage">
                  <el-input
                    v-model="queryParams.minDosage"
                    placeholder="请输入最小用量"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
                <el-form-item label="最大用量" prop="maxDosage">
                  <el-input
                    v-model="queryParams.maxDosage"
                    placeholder="请输入最大用量"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
                <el-form-item label="表类型" prop="deviceType">
                  <!-- <el-select
                    v-model="queryParams.maxDosage"
                    placeholder="请输入最大用量"
                    clearable
                    @keyup.enter.native="handleQuery"
                  /> -->
                  <el-select
                    v-model="queryParams.deviceType"
                    placeholder="请选择表类型"
                    clearable
                    style="width:100%"
                    @keyup.enter.native="handleQuery"
                  >
                    <el-option label="大用户" value="terminal" />
                    <el-option label="户表" value="meter" />
                  </el-select>
                </el-form-item>
                <el-form-item>
                </el-form-item>
              </div>
            </el-form>
          </el-col>
          <el-col class="form_query">
            <el-button v-show="collapseAble" type="text" @click="shiftCollapseAble" class="text">
              <span>
                {{ fold ? '收起筛选' : '更多筛选' }}
              </span>
            </el-button>
            <el-button type="primary" @click="handleQuery" size="mini">查询</el-button>
            <el-button @click="resetQuery" size="mini">重置</el-button>
          </el-col>
        </el-row>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['basedata:dosagerange:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['basedata:dosagerange:remove']"
            >批量删除</el-button>
          </el-col>
          <RightDropdown :columns="columns" :showSearch.sync="showSearch" @queryTable="getList" @export="handleExport" :hasPermiExport="['basedata:dosagerange:export']"></RightDropdown>
        </el-row>

        <el-table v-loading="loading" :data="dosagerangeList" border size="mini"
                  @selection-change="handleSelectionChange" ref="table" :height="getAvailableHeight">
          <el-table-column type="selection" width="55" align="center"/>
          <!-- <el-table-column label="平台主键" align="center" prop="id" v-if="false" :show-overflow-tooltip="true"/>
          <el-table-column label="租户ID" align="center" prop="tenantId" v-if="columns[0].visible" :show-overflow-tooltip="true"/> -->
          <el-table-column label="表类型" align="center" prop="deviceType" v-if="columns[0].visible" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span v-if="scope.row.deviceType === 'meter'">户表</span>
              <span v-if="scope.row.deviceType === 'terminal'">大用户表</span>
            </template>
          </el-table-column>
          <el-table-column label="用量类型" align="center" prop="dosageType" v-if="columns[1].visible" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <el-tag  v-if="scope.row.dosageType === 'day'">日用量</el-tag>
              <el-tag type="warning" v-if="scope.row.dosageType === 'month'">月用量</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="最小用量" align="center" prop="minDosage" v-if="columns[2].visible" :show-overflow-tooltip="true"/>
          <el-table-column label="最大用量" align="center" prop="maxDosage" v-if="columns[3].visible" :show-overflow-tooltip="true"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120px">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['basedata:dosagerange:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['basedata:dosagerange:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改用量区间设置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :label-width="labelWidth" :inline="true">
        <el-form-item label="归属水司" prop="tenantId">
          <el-select v-model="form.tenantId" placeholder="请选择归属水司" style="width: 202px;">
            <el-option
              v-for="dict in dict.type.belong_department"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value.toString()"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="表类型" prop="deviceType">
          <el-select
            v-model="form.deviceType"
            placeholder="请选择表类型"
            clearable
            style="width: 202px;"
          >
            <el-option label="大用户表" value="terminal" />
            <el-option label="户表" value="meter" />
          </el-select>
        </el-form-item>
        <el-form-item label="用量类型" prop="dosageType">
          <!-- <el-input v-model="form.dosageType" placeholder="请输入用量类型"/> -->
          <el-select v-model="form.dosageType" placeholder="请选择用量类型" style="width: 202px;">
            <el-option label="日用量" value="day" ></el-option>
            <el-option label="月用量" value="month"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="最小用量" prop="minDosage">
          <el-input v-model="form.minDosage" placeholder="请输入最小用量" />
        </el-form-item>
        <el-form-item label="最大用量" prop="maxDosage">
          <el-input v-model="form.maxDosage" placeholder="请输入最大用量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDosagerange, getDosagerange, delDosagerange, addDosagerange, updateDosagerange } from "@/api/basedata/dosagerange";

export default {
  dicts: ['belong_department'],
  name: "Dosagerange",
  data() {
    return {
      screenHeight: null,
      size:'small',
      collapseAble: false,
      fold: false,
      maxShow: '4',
      labelWidth: '90px',
      // 列信息
      columns: [
        // { key: 0, label: `平台主键`, visible: false },
        // { key: 0, label: `租户ID`, visible: true },
        { key: 0, label: `表类型`, visible: true },
        { key: 1, label: `用量类型`, visible: true },
        { key: 2, label: `最小用量`, visible: true },
        { key: 3, label: `最大用量`, visible: true },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用量区间设置表格数据
      dosagerangeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        // tenantId: null,
        deviceType: null,
        dosageType: null,
        minDosage: null,
        maxDosage: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        tenantId: [
          { required: true, message: "归属水司不能为空", trigger: "blur" }
        ],
        deviceType: [
          { required: true, message: "表类型不能为空", trigger: "change" }
        ],
        dosageType: [
          { required: true, message: "用量类型不能为空", trigger: "change" }
        ],
        minDosage: [
          { required: true, message: "最小用量不能为空", trigger: "blur" }
        ],
        maxDosage: [
          { required: true, message: "最大用量不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.screenHeight = document.body.clientHeight
  },
  computed: {
    getAvailableHeight() {
      return this.screenHeight - (this.showSearch ? 230 : 173)
    },
  },
  mounted() {
    // 通过最大显示个数控制展开/折叠
    if (this.maxShow > 0) {
      this.minShowControl()
    }
  },
  methods: {
    // 展开折叠按钮
    shiftCollapseAble() {
      this.fold = !this.fold
      this.minShowControl()
    },
    // 通过maxShow控制元素显示/折叠
    minShowControl() {
      const group = window.document.querySelectorAll(`#searchFilter .el-form-item.el-form-item--${this.size}`)
      const len = group?.length ? group?.length - 1 : 0
      if (this.maxShow < len) {
        group.forEach((item, index) => {
          if (index > this.maxShow - 1 && index < len) {
            item.hidden = !this.fold
          }
        })
        this.collapseAble = true
      } else {
        this.collapseAble = false
      }
    },
    /** 查询用量区间设置列表 */
    getList() {
      this.loading = true;
      listDosagerange(this.queryParams).then(response => {
        this.dosagerangeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tenantId: null,
        deviceType: null,
        dosageType: 'day',
        minDosage: null,
        maxDosage: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用量区间设置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDosagerange(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用量区间设置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            // this.form.dosageType = 'day'
            updateDosagerange(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDosagerange(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function() {
        return delDosagerange(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('basedata/dosagerange/export', {
        ...this.queryParams
      }, `dosagerange_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
  .form_col{
    width: 80%;
    ::v-deep .el-form {
      width: 100%;
    }
    ::v-deep .el-form-item {
      width: 25%;
    }
  }
  .form_query{
    width: 20%;
    text-align: right;
  }
  </style>
